<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 扫码支付
 */
namespace osc\payment\controller;
use osc\common\controller\Base;
use payment\weixin\WxPayApi;
use payment\weixin\WxPayConfig;
use payment\weixin\WxPayUnifiedOrder;
use payment\weixin\WxPayNotifyCallBack;
use think\Db;

class TuanWeixin extends Base{
	
	//团购下单处理
	function process($id){
		return array('type'=>'wx_pay','url'=>url('payment/TuanWeixin/code',array('id'=>$id)));
	}	
	
	function code(){
		
		$order=osc_service('common','Tuan')->add_order(input('param.id'),session('shipping_address_id'),session('payment_method'));
		
		if($order['order_id']){
		
			$config=payment_config('weixin');
			
			$cfg = array(
			    'APPID'     => $config['appid'],
			    'MCHID'     => $config['weixin_partner'],
			    'KEY'       => $config['partnerkey'],
			    'APPSECRET' => $config['appsecret'],
			    'NOTIFY_URL' =>request()->domain().url('payment/Weixin/weixin_notify')
		    );
		    WxPayConfig::setConfig($cfg);     
	        //②、统一下单
	        $input = new WxPayUnifiedOrder();           
	  
	        $input->SetBody($order['subject']);
	        $input->SetAttach('附加数据');
	        $input->SetOut_trade_no($order['pay_order_no']);
			
	        $input->SetTotal_fee((float)$order['pay_total']*100);
			
	        $input->SetTime_start(date("YmdHis"));
	        $input->SetTime_expire(date("YmdHis", time() + 600));
			$input->SetTrade_type('NATIVE');
	
			$input->SetProduct_id(time());
			
			$wxapi=new WxPayApi();
			
		    $url= $wxapi->unifiedOrder($input);	
			//删除购物车中的记录
			osc_service('common','Tuan')->clear_cart($order['uid']);				
			
			$this->assign('url',$url['code_url']);
			
			$this->assign('trade_no',$order['pay_order_no']);
			
		}
		
		return $this->fetch('weixin:code'); 
	}



}
